package com.ea.eadp.nexus.test.pageDefinition.gameconnect;

import com.ea.eadp.nexus.test.common.PageOperation;
import com.ea.eadp.nexus.test.pageDefinition.BasePage;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;


public class GameConnectSignupPage extends BasePage {
    public GameConnectSignupPage(WebDriver driver) {
        super(driver, GameConnectSignupPage.class);
    }

    public void navigate(String uri) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(uri);
        driver.navigate().to(uri);
    }

    public void setEmail(String email) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(email);
        PageOperation.SendKeyToElementAfterWait(driver, "email", email);
    }

    public void setOriginId(String originID) {
        driver.switchTo().window(this.operationWindowHandle);
        PageOperation.SendKeyToElementAfterWait(driver, "originId", originID);
    }

    public void setPassword(String password) {
        driver.switchTo().window(this.operationWindowHandle);
        PageOperation.SendKeyToElementAfterWait(driver, "password", password);
    }

    public void setConfirmPassword(String confirmPassword) {
        driver.switchTo().window(this.operationWindowHandle);
        PageOperation.SendKeyToElementAfterWait(driver, "confirmPassword", confirmPassword);
    }

    public void setSecurityQuestion(String question) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        PageOperation.SelectCssDropDownItem(driver, "#clientreg_securityqa", question);
    }

    public void setSecurityAnswer(String answer) {
        driver.switchTo().window(this.operationWindowHandle);
        PageOperation.SendKeyToElementAfterWait(driver, "securityAnswer", answer);
    }

    public void setRegisterCountry(String country) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        PageOperation.SelectCssDropDownItem(driver, "#clientreg_country", country);
    }

    public void setDobMonth(String month) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        PageOperation.SelectCssDropDownItem(driver, "#clientreg_dobmonth", month);
    }

    public void setDobDay(String day) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        PageOperation.SelectCssDropDownItem(driver, "#clientreg_dobday", day);
    }

    public void setDobYear(String year) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        PageOperation.SelectCssDropDownItem(driver, "#clientreg_dobyear", year);
    }

    public void setCaptcha(String value) {
        driver.switchTo().window(this.operationWindowHandle);
        PageOperation.SendKeyToElementAfterWait(driver, "recaptcha_response_field", value);
    }

    public void setContactMe(boolean check) {
        driver.switchTo().window(this.operationWindowHandle);
        WebElement checkbox = driver.findElement(By.id("contactMe"));
        WebElement parent = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].parentNode;", checkbox);
        if (check && !checkbox.isSelected()) {
            parent.click();
        } else if (!check && checkbox.isSelected()) {
            parent.click();
        }
    }

    public void setReadAccept(boolean check) {
        driver.switchTo().window(this.operationWindowHandle);
        WebElement checkbox = driver.findElement(By.id("readAccept"));
        WebElement parent = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].parentNode;", checkbox);
        if (check && !checkbox.isSelected()) {
            parent.click();
        } else if (!check && checkbox.isSelected()) {
            parent.click();
        }
    }

    public String getMessageByXPath(String xpath) {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath(xpath)).getText();
    }

    public void clickToLink() {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//div[@id='panel-profile-confirmation']/div[4]/div[2]/a/span/span")).click();
    }

    public void clickToSignUp() {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//form[@id='regForm']/div[9]/a/span/span")).click();
    }

    public void clickToEntry() {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//div[@id='panel-underage']/div[2]/a/span/span")).click();
    }
}
